package com.farm.shop.dao;


import com.farm.shop.domain.FarmShopDO;
import com.farm.shop.response.FarmShop;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 农家店铺表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2019-09-06 19:44:50
 */
@Mapper
public interface FarmShopDao {

    FarmShopDO get(Long id);

    List<FarmShopDO> list(Map<String, Object> map);

    int count(Map<String, Object> map);

    int save(FarmShopDO farmShop);

    int update(FarmShopDO farmShop);

    int remove(Long id);

    int batchRemove(Long[] ids);

    /**
     * 审核通过
     *
     * @return 影响的行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Update(value = "UPDATE farm_shop SET `status` = 1  WHERE id = #{id}")
    int updateStatusById(@Param("id") Long id);

    /**
     * 首页搜索
     *
     * @param keyword 参数
     * @return
     */
    List<FarmShopDO> search(@Param("farmName") String keyword);

    /**
     *  查询审核列表
     *
     * @param map
     * @return
     */
    List<FarmShopDO> listShopReview(Map<String,Object> map);

    /**
     *  审核通过
     *
     *  饼状图  查询所有的农家数量
     *  按照效率 尽量使用 count(*)
     *  count（*） > count（1） > count（id） > count（字段）
     *
     * @return  count
     */
    @Select(value = "SELECT COUNT(*) FROM farm_shop WHERE status = 1")
    int shopCount();

    /**
     *  饼状图 查询只包含住宿的农家数量
     *
     * @return  count
     */
    @Select(value = "SELECT COUNT(*) FROM farm_shop WHERE accommodation = 1 AND dining_room = 2 AND status = 1")
    int accomCount();

    /**
     *  饼状图 查询只包含美食的农家数量
     *
     * @return  count
     */
    @Select(value = "SELECT COUNT(*) FROM farm_shop WHERE accommodation = 2 AND dining_room = 1 AND status = 1 ")
    int diningCount();

    /**
     *  饼状图 查询同时包含美食的农家数量
     *
     * @return  count
     */
    @Select(value = "SELECT COUNT(*) FROM farm_shop WHERE accommodation = 1 AND dining_room = 1  AND status = 1 ")
    int diningAndAccomCount();

    /**
     *  折线图  住宿农家数据
     *
     * @return List<FarmShop>
     */
    @Select(value = "SELECT create_time FROM farm_shop WHERE accommodation = 1 and dining_room =2 AND status = 1")
    List<FarmShopDO> getAccoms();

    /**
     *  折线图  美食农家数据
     *
     * @return List<FarmShop>
     */
    @Select(value = "SELECT create_time FROM farm_shop WHERE accommodation = 2 and dining_room =1 AND status = 1")
    List<FarmShopDO> getDining();

    /**
     *  折线图  同时包含住宿和美食
     *
     * @return List<FarmShop>
     */
    @Select(value = "SELECT create_time FROM farm_shop WHERE accommodation = 1 and dining_room =1 AND status = 1")
    List<FarmShopDO> getAccomAndDinings();

    /**
     * 审核通过 拒绝
     *
     * @return 影响的行数
     */
    @Transactional(rollbackFor = Exception.class)
    @Update(value = "UPDATE farm_shop SET `status` = 3  WHERE id = #{id}")
    int nopassStatusById(Long id);

    /**
     *  用户与店铺的关系
     *
     * @param userId  用户id
     * @param id      店铺id
     * @return       int
     */
    @Transactional(rollbackFor = Exception.class)
    @Update(value = "UPDATE farm_shop SET `user_id` = #{userId}  WHERE id = #{id}")
    int passStatusAndUserId(@Param("userId") Long userId ,@Param("id") Long id );

    /**
     *  根据 userId 查询店铺 id
     *
     * @param userId  用户id
     * @return Long
     */
    @Select(value = "SELECT id  FROM farm_shop WHERE `user_id` = #{userId} ")
    Long getShopId(@Param("userId") Long userId );

    /**
     *  根据 userId 查询店铺信息
     * @param userId userId
     * @return  FarmShopDO
     */
    @Select(value = "SELECT *  FROM farm_shop WHERE `user_id` = #{userId} ")
    List<FarmShopDO> getFarmShopByUserId(Long userId);
}
